package com.seal.yoyoshop.service.impl;

import com.seal.yoyoshop.bin.Goods;
import com.seal.yoyoshop.bin.Top;
import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.dao.GoodsDao;
import com.seal.yoyoshop.dao.TypeDao;
import com.seal.yoyoshop.dao.impl.GoodsDaoImpl;
import com.seal.yoyoshop.dao.impl.TypeDaoImpl;
import com.seal.yoyoshop.service.GoodsService;
import com.seal.yoyoshop.service.TopService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImpl();
    private TypeDao typeDao = new TypeDaoImpl();
    private TopService topService = new TopServiceImpl();

    @Override
    public List<Goods> getGoodsListByHotType(int type) {
        List<Goods> goodsList = goodsDao.selectGoodsByHotType(type);
        for(Goods goods:goodsList)
        {
            Integer typeId = goods.getTypeId();
            Type t = typeDao.selectTypeById(typeId);
            goods.setType(t);
        }
        return goodsList;
    }

    @Override
    public List<Goods> getGoodsListByTypeId(int typeId) {
        List<Goods> goodsList = goodsDao.selectGoodsByTypeId(typeId);
        return goodsList;
    }

    @Override
    public List<Goods> getGoodsListByTypeId(int typeId, int page, int size) {
        List<Goods> goodsList = goodsDao.selectGoodsByTypeId(typeId,page,size);
        return goodsList;
    }

    @Override
    public long getCountByTypeId(int typeId) {
        long total = goodsDao.countByTypeId(typeId);
        return total;
    }

    @Override
    public long getCountByHotType(int typeId) {
        long total = goodsDao.countByHotType(typeId);
        return total;
    }

    @Override
    public List<Goods> getGoodsListByHotType(int typeId, int page, int size) {
        List<Goods> goodsList = goodsDao.selectGoodsByHotType(typeId,page,size);
        for(int i = 0; i < goodsList.size(); i ++ ){
            Goods goods = goodsList.get(i);
            Integer typeid = goods.getTypeId();
            Type type = typeDao.selectTypeById(typeid);
            goods.setType(type);

            //        查商品属于哪个榜单
            int goodsId = goods.getId();
            List<Top> topList = topService.getTopListByGoodId(goodsId);
//            获取榜单类型
            for(int j = 0; j < topList.size(); j ++ ){
                Top top = topList.get(j);
                int topType = top.getType();
                switch (topType){
                    case 1:
                        goods.setTopToday(true);
                        break;
                    case 2:
                        goods.setTopHot(true);
                        break;
                    case 3:
                        goods.setTopNew(true);
                        break;
                }
            }
        }
        return goodsList;
    }

    @Override
    public List<Goods> getGoodsListBySearch(String keyword, int page, int size) {
        List<Goods> goodsList = goodsDao.selectGoodsByKeyWord(keyword,page,size);
        return goodsList;
    }

    @Override
    public long getCountBySearch(String keyword) {
        long total = goodsDao.countByKeyword(keyword);
        return total;
    }

    @Override
    public Goods getGoodById(int goodId) {
//        获取good信息
        Goods good = goodsDao.selectGoodById(goodId);
//        获取分类信息
        int typeId = good.getTypeId();
        Type type = typeDao.selectTypeById(typeId);
        good.setType(type);
        return good;
    }

    @Override
    public List<Goods> getGoodList(Integer page, Integer size) {
//        获取商品列表
        List<Goods> goodsList = goodsDao.selectGoodsList(page,size);
        for(int i = 0; i < goodsList.size(); i ++ ){
            Goods goods = goodsList.get(i);
            Integer typeid = goods.getTypeId();
            Type type = typeDao.selectTypeById(typeid);
            goods.setType(type);

            //        查商品属于哪个榜单
            int goodsId = goods.getId();
            List<Top> topList = topService.getTopListByGoodId(goodsId);
//            获取榜单类型
            for(int j = 0; j < topList.size(); j ++ ){
                Top top = topList.get(j);
                int topType = top.getType();
                switch (topType){
                    case 1:
                        goods.setTopToday(true);
                        break;
                    case 2:
                        goods.setTopHot(true);
                        break;
                    case 3:
                        goods.setTopNew(true);
                        break;
                }
            }

        }


        return goodsList;
    }

    @Override
    public long getGoodsCount() {
        long count = goodsDao.selectGoodsCount();
        return count;
    }

    @Override
    public boolean addGoods(Goods goods) {
        boolean isSuccess = goodsDao.addGoodsByGoods(goods);
        return isSuccess;
    }

    @Override
    public boolean deleteGoodsById(int goodsId) {
        boolean isSuccess = goodsDao.addGoodsByGoods(goodsId);
        return isSuccess;
    }

    @Override
    public boolean updateGoods(Goods goods) {
        boolean isSuccess = goodsDao.updateGoodsByGoods(goods);
        return isSuccess;
    }
}
